package dataTypes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;

/* 
 * The deal DB will hold all deals in the system with all the relevant info and 
 * a unique id.
 * This id allows us to retrieve all the users who sighed to the deal from
 * activeDeals DB.
 * 
 *  Each deal has also a unique business id to retrieve business info from\
 *  business DB.
 *  
 *  index, business id, start time, end time, num of coupons, max num per user,
 *  deal type, full amount, discount percent, isActive 
 *  
 *  types:
 *  1. x% discount
 *  2. buy x get y free
 * 
 */

@PersistenceCapable(detachable = "true")
public class deal implements Comparable<deal>{

	public deal( 
			int numOfCoupons, int maxPerUser, int dealType, int amount,
			int percent, boolean isActive, Date start, Date end) {
		this.start = start;
		this.end = end;
		this.numOfCoupons = numOfCoupons;
		this.maxPerUser = maxPerUser;
		this.dealType = dealType;
		this.amount = amount;
		this.percent = percent;
		this.isActive = isActive;
		this.numPurchased = 0;
		this.numUsed = 0;
		this.start = start;
		this.end = end;
		this.coupons=new ArrayList<activeUserDeals>();
	}

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent(defaultFetchGroup="true")
	private business business;

	@Persistent
	private Date start;

	@Persistent
	private Date end; 

	@Persistent
	private int numOfCoupons;

	@Persistent
	private int numPurchased;
	
	@Persistent
	private int numUsed;
	

	@Persistent
	private int maxPerUser;

	@Persistent
	private int dealType;

	@Persistent
	private int amount;

	@Persistent
	private int percent;

	@Persistent
	private boolean isActive;
	

	@Persistent (mappedBy = "deal",defaultFetchGroup="true")
	private List<activeUserDeals> coupons;


	
	public int getNumPurchased() {
		return numPurchased;
	}

	public void setNumPurchased(int numPurchased) {
		this.numPurchased = numPurchased;
	}
	
	public int getNumUsed() {
		return numUsed;
	}

	public void setNumUsed(int numPurchased) {
		this.numUsed = numPurchased;
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public business getBusiness() {
		return business;
	}

	public void setBusiness(business business) {
		this.business = business;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}
	
	
	public int getNumOfCoupons() {
		return numOfCoupons;
	}

	public void setNumOfCoupons(int numOfCoupons) {
		this.numOfCoupons = numOfCoupons;
	}

	public int getMaxPerUser() {
		return maxPerUser;
	}

	public void setMaxPerUser(int maxPerUser) {
		this.maxPerUser = maxPerUser;
	}

	public int getDealType() {
		return dealType;
	}

	public void setDealType(int dealType) {
		this.dealType = dealType;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public int getPercent() {
		return percent;
	}

	public void setPercent(int percent) {
		this.percent = percent;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public void setCoupons(List<activeUserDeals> coupons) {
		this.coupons = coupons;
	}
	
	public void addCoupon(activeUserDeals a) {
		this.coupons.add(a);
		setNumPurchased(getNumPurchased()+1);
	}

	public List<activeUserDeals> getCoupons() {
		return coupons;
	}

	@Override
	public int compareTo(deal o) {
		
		return this.getKey().compareTo(o.getKey());
	}
}




